package org.sdfc.query

import com.documentum.fc.client.IDfSession
import com.documentum.fc.common.DfException

import org.sdfc.model.dfc.collection.RichCollection

/** Replacement for Documentum class DfQuery.
 */
class Query (
  /** DQL of the query. */
  val dql : String
) {
  
  /** Execute this with the given queryType and return the results in a RichCollection.
   */
  @throws(classOf[DfException])
  def execute(session : IDfSession, queryType : Int) : RichCollection =
    toTypedQuery(queryType).execute(session)
  
  
  /** Prepares this for execution in a PreparedQuery which provides useful extraction methods.
   */
  def prepare(session : IDfSession, queryType : Int) : PreparedQuery =
    toTypedQuery(queryType).prepare(session)
  
  
  /** Prepares this as a sequence of at most one CollectionRow usablewith sequence comprehension syntax.
   */
  def firstRow(session : IDfSession, queryType : Int) : PreparedFirstRow =
    toTypedQuery(queryType).firstRow(session)
  
  /** Prepares this as a sequence of CollectionRow usable with sequence comprehension syntax.
   */
  def rows(session : IDfSession, queryType : Int) : PreparedRows =
    toTypedQuery(queryType).rows(session)
  
  
  /** Binds the dql from this and the given queryType into a new TypedQuery.
   */
  protected def toTypedQuery(targetType : Int) : TypedQuery =
    new TypedQuery(dql) {
      def queryType = targetType
    }
  
}
